home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / PROGRAMM / PASCAL / 0187.ZIP / CRIBBAGE.PS3 < prev    next >
Text File  |  1985-01-20  |  4KB  |  166 lines

  1. { Included File: SCORE.INC }
  2.  
  3. function score(hand: handtype):integer;
  4. const jack = 11;
  5. var
  6.   index: 1..5;
  7.   return: integer;
  8.   cardmax: 4..5;
  9.  
  10.   function fifteen(hand: handtype):integer;
  11.   var
  12.     ways: integer;
  13.     i,j,k: 1..dealsize;
  14.     sum: integer;
  15.   begin
  16.     sum:=0;
  17.     for i:=1 to cardmax do
  18.       begin
  19.         if hand[i].rank>10 then hand[i].rank:=10;
  20.         sum:=sum+hand[i].rank
  21.       end;
  22.     ways:=0;
  23.     if sum=15 then ways:=1;
  24.     for i:=1 to cardmax do
  25.       begin
  26.         with hand[i] do
  27.           begin
  28.             if (sum-rank) <= 15 then
  29.               begin
  30.                 if (sum-rank) = 15 then ways:=ways+1
  31.               end
  32.             else
  33.               for j:=i+1 to cardmax do   { ????????????? }
  34.                 begin
  35.                   if (sum-rank-hand[j].rank)<=15 then
  36.                     begin
  37.                       if (sum-rank-hand[j].rank)=15 then
  38.                         ways:=ways+1
  39.                     end
  40.                   else
  41.                     for k:=j+1 to cardmax do
  42.                       if (sum-rank-hand[j].rank-hand[k].rank)=15 then
  43.                         ways:=ways+1
  44.                 end
  45.           end {with}
  46.       end;
  47.     fifteen:=ways*2
  48.   end;  {fifteen}
  49.  
  50.   function run(hand: handtype):integer;
  51.   var
  52.     mult: integer;
  53.     matched: 0..5;
  54.     i: 1..dealsize;
  55.     return: integer;
  56.     seen: boolean;
  57.   begin
  58.     mult:=1;
  59.     matched:=0;
  60.     return:=0;
  61.     seen:=false;
  62.     for i:=1 to (cardmax-1) do
  63.       begin
  64.         if hand[i].rank=hand[i+1].rank then
  65.           begin
  66.             if seen then mult:=mult+1
  67.             else mult:=mult+2;
  68.             seen:=true;
  69.             if i=(cardmax-1) then
  70.               begin
  71.                 if mult>=2 then mult:=mult-1;
  72.                 if matched>=2 then
  73.                   return:=return+((matched+1)*mult);
  74.                 matched:=0
  75.               end
  76.           end
  77.         else
  78.           begin
  79.             seen:=false;
  80.             if hand[i].rank=(hand[i+1].rank-1) then
  81.               matched:=matched+1
  82.             else
  83.               begin
  84.                 if mult>=2 then mult:=mult-1;
  85.                 if matched>=2 then
  86.                   return:=return+((matched+1)*mult);
  87.                 mult:=1;
  88.                 matched:=0
  89.               end
  90.           end
  91.       end;
  92.     if mult>=2 then mult:=mult-1;
  93.     if matched>=2 then
  94.       return:=return+((matched+1)*mult);
  95.     run:=return
  96.   end;  {run}
  97.  
  98.   function pair(hand: handtype):integer;
  99.   var
  100.     return: integer;
  101.     matched: 0..4;
  102.     index: 1..dealsize;
  103.   begin
  104.     return:=0;
  105.     matched:=0;
  106.     for index:=1 to (cardmax-1) do
  107.       begin
  108.         if hand[index].rank=hand[index+1].rank then
  109.           matched:=matched+1;
  110.         if (hand[index].rank<>hand[index+1].rank) or
  111.            (index=cardmax-1) then
  112.            begin
  113.              case matched of
  114.                0: ;
  115.                1: return:=return+2;
  116.                2: return:=return+6;
  117.                3: return:=return+12
  118.                end; {case}
  119.              matched:=0     { ??? }
  120.            end
  121.       end;
  122.     pair:=return
  123.   end;  {pair}
  124.  
  125.   function flush(hand: handtype):integer;
  126.   var
  127.     return: integer;
  128.     index: 1..dealsize;
  129.   begin
  130.     return:=1;
  131.     for index:=1 to 3 do
  132.       if hand[index].suit=hand[index+1].suit then
  133.         return:=return+1;
  134.     if return=4 then
  135.       begin
  136.         if common.rank<>0 then
  137.           if common.suit=hand[4].suit then return:=5;
  138.         flush:=return
  139.       end
  140.     else flush:=0
  141.   end;  {flush}
  142.  
  143. BEGIN  {score}
  144.   return:=0;
  145.   cardmax:=4;
  146.   return:=return + flush(hand);
  147.   if common.rank<>0 then
  148.     begin
  149.       cardmax:=5;
  150.       for index := 1 to playsize do
  151.         if hand[index].rank = jack then
  152.            if hand[index].suit = common.suit then
  153.               return:=return+1;  {GLITCH IN THE BOOK???}
  154.       hand[5]:=common;
  155.       sort(5,hand)
  156.     end
  157.   else
  158.     begin
  159.       hand[5].rank:=0
  160.     end;
  161.   return:= fifteen(hand)+return;
  162.   return:= pair(hand)+return;
  163.   return:= run(hand)+return;
  164.   score:=return
  165. END;   {score}
  166.